package com.housekeeping.utils;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

/**
 * 数据库工具类
 */
public class DBUtils {
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    
    private static QueryRunner queryRunner = new QueryRunner();

    static {
        loadConfig();
    }

    
    private static void loadConfig() {
        Properties props = new Properties();
        InputStream in = DBUtils.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            props.load(in);
            driver = props.getProperty("db.driver");
            url = props.getProperty("db.url");
            username = props.getProperty("db.username");
            password = props.getProperty("db.password");
            Class.forName(driver);
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException("数据库配置加载失败", e);
        }
    }

    
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }

    
    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    
    public static <T> T queryForObject(String sql, Class<T> clazz, Object... params) {
        Connection conn = null;
        try {
            conn = getConnection();
            ResultSetHandler<T> handler = new BeanHandler<>(clazz);
            return queryRunner.query(conn, sql, handler, params);
        } catch (SQLException e) {
            throw new RuntimeException("查询失败", e);
        } finally {
            closeConnection(conn);
        }
    }

    
    public static <T> List<T> queryForList(String sql, Class<T> clazz, Object... params) {
        Connection conn = null;
        try {
            conn = getConnection();
            ResultSetHandler<List<T>> handler = new BeanListHandler<>(clazz);
            return queryRunner.query(conn, sql, handler, params);
        } catch (SQLException e) {
            throw new RuntimeException("查询失败", e);
        } finally {
            closeConnection(conn);
        }
    }

    
    public static Object queryForScalar(String sql, Object... params) {
        Connection conn = null;
        try {
            conn = getConnection();
            ResultSetHandler<Object> handler = new ScalarHandler<>();
            return queryRunner.query(conn, sql, handler, params);
        } catch (SQLException e) {
            throw new RuntimeException("查询失败", e);
        } finally {
            closeConnection(conn);
        }
    }

    
    public static int update(String sql, Object... params) {
        Connection conn = null;
        try {
            conn = getConnection();
            return queryRunner.update(conn, sql, params);
        } catch (SQLException e) {
            throw new RuntimeException("更新失败", e);
        } finally {
            closeConnection(conn);
        }
    }

    
    public static int[] batchUpdate(String sql, Object[][] params) {
        Connection conn = null;
        try {
            conn = getConnection();
            return queryRunner.batch(conn, sql, params);
        } catch (SQLException e) {
            throw new RuntimeException("批量更新失败", e);
        } finally {
            closeConnection(conn);
        }
    }
} 